<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreCalendar {
	var $today;
	var $year;
	var $month;
	var $day;
	var $days;
	var $day_week;
	var $startday_week;
	var $endday_week;
	
	var $time;
	var $stats;
	
	//constructor
	function CoreCalendar() {
		$this->time = time();
	}
	
	//setter/getter
	function setTime($time = '') {
		if( $time == '' ) {
			$this->time = time();
		} else {
			$this->time = $time;
		}
	}
	
	function getTime() {
		return $time;
	}
	
	function getYear() {
		return date("Y", $this->time);
	}
	
	function getMonth() {
		return date("m", $this->time);
	}
	
	function getDay() {
		return date("j", $this->time);
	}
	
	function getDayWeek() {
		return date("w", $this->time);
	}
	
	function getTitle() {
		return date("M Y", $this->time);
	}
	
	function parse() {
		$this->title = date("M Y", $this->time);
		$this->year = date("Y", $this->time);
		$this->month = date("m", $this->time);
		$this->day = date("j", $this->time);
		$this->days = date("t", $this->time);
		$this->day_week = date("w", $this->time);
		$this->startday_week = date("w", mktime(0, 0, 0, $this->month, $this->day, $this->year));
		$this->endday_week = date("w", mktime(0, 0, 0, $this->month + 1, $this->day, $this->year));
	}
	
	function output() {
		$content = $this->_js($this->time) . "\n" . $this->_html($this->time);
		
		return $content;
	}
	
	//
	function _html($time) {
		//$time = time();
		//$date = strftime("%Y%m", $time);
		$title = date("M Y", $time);
		$year = date("Y", $time);
		$month = date("m", $time);
		$day = date("j", $time);
		$days = date("t", $time);
		$day_week = date("w", $time);
		$startday_week = date("w", mktime(0, 0, 0, $month, 1, $year));
		$endday_week = date("w", mktime(0, 0, 0, $month + 1, 1, $year));
		
		$startday = 0 - $startday_week;
		$endday =  $days - $endday_week + 7;
		
		$text = array();
		$style = array();
		for($i=$startday; $i<=$endday; $i++) {
			if( $i <= 0 ) {
				$text[$i] = date("j", mktime(0, 0, 0, $month, $i, $year));
				$style[$i] = "lmd";
			} elseif( $i > $days ) {
				$text[$i] = date("j", mktime(0, 0, 0, $month, $i, $year));
				$style[$i] = "nmd";
			} else {
				if( $day == $i ) {
					$text[$i] = "<b>" . $i . "</b>";
					$style[$i] = "today";
				} else {
					$text[$i] = $i;
					$style[$i] = "md";
				}
			}
		}
		
		$content = '';
		$content .= '<div class="infoBox">' . "\n";
		$content .= '<div class="infoBoxContent">' . "\n";
		$content .= '<div class="infoBoxItem">' . "\n";
		$content .= '<table width="100%" border="0" cellspacing="0" cellpadding="0" class="calendar">' . "\n";
		$content .= '<tr class="nav">';
		$content .= '<td align="left"><a href="javascript: //" onClick="pp_calendar_previousmonth()">&lt;</a></td>';
		$content .= '<td align="center" colspan="5">' . $title . '</td>';
		$content .= '<td align="right"><a href="javascript: //" onClick="pp_calendar_nextmonth()">&gt;</a></td>';
		$content .= '</tr>' . "\n";
		
		$content .= '<tr class="week">';
		$content .= '<td>Sun</td>';
		$content .= '<td>Mon</td>';
		$content .= '<td>Tue</td>';
		$content .= '<td>Wed</td>';
		$content .= '<td>Thu</td>';
		$content .= '<td>Fri</td>';
		$content .= '<td>Sat</td>';
		$content .= '</tr>' . "\n";
		
		for($i=$startday; $i<$endday; $i=$i+7) {
			$content .= '<tr>';
			$content .= '<td class="' . $style[($i + 1)] . '">' . $text[($i + 1)] . '</td>';
			$content .= '<td class="' . $style[($i + 2)] . '">' . $text[($i + 2)] . '</td>';
			$content .= '<td class="' . $style[($i + 3)] . '">' . $text[($i + 3)] . '</td>';
			$content .= '<td class="' . $style[($i + 4)] . '">' . $text[($i + 4)] . '</td>';
			$content .= '<td class="' . $style[($i + 5)] . '">' . $text[($i + 5)] . '</td>';
			$content .= '<td class="' . $style[($i + 6)] . '">' . $text[($i + 6)] . '</td>';
			$content .= '<td class="' . $style[($i + 7)] . '">' . $text[($i + 7)] . '</td>';
			$content .= '</tr>' . "\n";
		}
		
		$content .= '</table>' . "\n";
		$content .= '</div></div></div>' . "\n";
		
		return $content;
	}
	
	function _js($time) {
		$year = date("Y", $time);
		$month = date("m", $time);
		$day = date("j", $time);
		
		$content = '';
		$content .= '<script language="javascript" type="text/javascript">' . "\n";
		$content .= '<!--' . "\n";
		$content .= 'var pp_calendar_year = ' . $year . ";\n";
		$content .= 'var pp_calendar_month = ' . $month . ";\n";
		$content .= 'var pp_calendar_day = ' . $day . ";\n";
		$content .= 'function pp_calendar_nextmonth() {' . "\n";
		$content .= '	if( typeof(calendar_nextmonth) == "function" ) {' . "\n";
		$content .= '		calendar_nextmonth();' . "\n";
		$content .= '	}' . "\n";
		$content .= '}' . "\n";
		$content .= 'function pp_calendar_previousmonth() {' . "\n";
		$content .= '	if( typeof(calendar_previousmonth) == "function" ) {' . "\n";
		$content .= '		calendar_previousmonth();' . "\n";
		$content .= '	}' . "\n";
		$content .= '}' . "\n";
		$content .= '-->' . "\n";
		$content .= '</script>' . "\n";
		
		return $content;
	}
	
	function _count($month) {
		
	}
}
//
?>